package org.evan.spring6.aop.xmlaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//切面类
@Component
public class LogAspect {

    //前置通知
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("Logger-->前置通知, 方法名称: " + methodName + ", 参数: " + Arrays.toString(args));
    }

    //返回(AfterReturning)
    public void afterReturningMethod(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->返回通知, 方法名称: " + methodName + ", 返回结果: " + result);
    }

    //异常(AfterThrowing)获取目标方法异常信息
    //目标方法出现异常，这个通知执行
    public void afterThrowingMethod(JoinPoint joinPoint,Throwable e){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->异常通知, 方法名称: " + methodName + ", 异常信息: " + e);
    }

    //后置(After)
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->后置通知, 方法名称: " + methodName);
    }

    //环绕(Around)
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
        String methodName = proceedingJoinPoint.getSignature().getName();
        Object[] args =proceedingJoinPoint.getArgs();
        String argString = Arrays.toString(args);
        Object result = null;
        try {
            System.out.println("环绕通知==目标方法之前执行");

            //调用目标方法
            result = proceedingJoinPoint.proceed();

            System.out.println("环绕通知==目标方法返回值之后执行");
        }catch (Throwable throwable){
            throwable.fillInStackTrace();
            System.out.println("环绕通知==目标方法出现异常之后执行");
        }finally {
            System.out.println("环绕通知==目标方法执行完毕执行");
        }
        return result;
    }

    //重用切入点表达式
    public void pointCut(){}
}
